package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.ConfigRegion;
import com.ruoyi.common.core.domain.entity.Region;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.service.IRegionService;

/**
 * 区域Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-22
 */
@Service
public class RegionServiceImpl implements IRegionService 
{
    @Autowired
    private RegionMapper regionMapper;

    /**
     * 查询区域
     * 
     * @param id 区域主键
     * @return 区域
     */
    @Override
    public Region selectRegionById(Long id)
    {
        return regionMapper.selectRegionById(id);
    }

    /**
     * 查询区域列表
     * 
     * @param region 区域
     * @return 区域
     */
    @Override
    public List<Region> selectRegionList(Region region)
    {
        return regionMapper.selectRegionList(region);
    }

    /**
     * 新增区域
     * 
     * @param region 区域
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRegion(Region region)
    {
        region.setCreateTime(DateUtils.getNowDate());
        int rows = regionMapper.insertRegion(region);
        insertConfigRegion(region);
        return rows;
    }

    /**
     * 修改区域
     * 
     * @param region 区域
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRegion(Region region)
    {
        region.setUpdateTime(DateUtils.getNowDate());
        regionMapper.deleteConfigRegionByCityCode(region.getId());
        insertConfigRegion(region);
        return regionMapper.updateRegion(region);
    }

    /**
     * 批量删除区域
     * 
     * @param ids 需要删除的区域主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRegionByIds(Long[] ids)
    {
        regionMapper.deleteConfigRegionByCityCodes(ids);
        return regionMapper.deleteRegionByIds(ids);
    }

    /**
     * 删除区域信息
     * 
     * @param id 区域主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRegionById(Long id)
    {
        regionMapper.deleteConfigRegionByCityCode(id);
        return regionMapper.deleteRegionById(id);
    }

    /**
     * 新增区域业务配置信息
     * 
     * @param region 区域对象
     */
    public void insertConfigRegion(Region region)
    {
        List<ConfigRegion> configRegionList = region.getConfigRegionList();
        Long id = region.getId();
        if (StringUtils.isNotNull(configRegionList))
        {
            List<ConfigRegion> list = new ArrayList<ConfigRegion>();
            for (ConfigRegion configRegion : configRegionList)
            {
                configRegion.setCityCode(String.valueOf(id));
                list.add(configRegion);
            }
            if (list.size() > 0)
            {
                regionMapper.batchConfigRegion(list);
            }
        }
    }
}
